package palms.client.android.overlay;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.LinkedList;

import palms.client.android.apis.view.PalmsMapView;
import palms.client.domain.User;
import palms.location.results.ALocation;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class ResultsOverlay extends Overlay {

	private User myuser;
	private PalmsMapView mymap;
	
	public ResultsOverlay(User u, PalmsMapView m) {   
		this.myuser = u;
		this.mymap = m;
	}  
	
	public boolean onTap(GeoPoint p, MapView mapView)  {

		for(ALocation l : this.myuser.getRes()){
			
			GeoPoint n = new GeoPoint((int)(l.getLat()*1E6), (int)(l.getLog()*1E6));
			if(Math.abs((n.getLatitudeE6() - p.getLatitudeE6())) <= 100 &&
			  Math.abs((n.getLongitudeE6() - p.getLongitudeE6())) <= 100){
				try {
					this.mymap.CallPlaceInfo(l);
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
        
        return true;
    }
	
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {  
	    
		LinkedList<ALocation> res = this.myuser.getRes();
		
		if(res==null)
			return;
		
		Projection p = mapView.getProjection();
		Paint paint = new Paint();
		Point point = new Point();
		paint.setColor(Color.GREEN);
		paint.setStrokeWidth(5); 
		paint.setAlpha(200);
		
		for(ALocation l : res){
			GeoPoint gp = new GeoPoint((int)(l.getLat()*1E6), (int)(l.getLog()*1E6));
			p.toPixels(gp, point);
			canvas.drawCircle(point.x, point.y, 5, paint);
		}
		
		
		super.draw(canvas, mapView, shadow);
		
	}
	
}